package ex2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFrame;

public class Server extends JFrame{

	
	public static boolean active;
	
	public Server(){
		super("Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(200,100);
		setVisible(true);
		try{
			ExecutorService pool = Executors.newCachedThreadPool();
			ServerSocket server = new ServerSocket(4242);
			active = true;
			System.out.println("Server started");
			while(active){
				Socket client = server.accept();
				System.out.println("Client accepted");
				Request r = new Request(client);
				pool.execute(r);
				Thread.sleep(150);
			}
			System.out.println("Server stopped");
			server.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Server server = new Server();
	}

}
